package cn.shanke.torrentmanagerserver.controller;

import cn.shanke.torrentmanagerserver.controller.common.ResponseModel;
import cn.shanke.torrentmanagerserver.model.BTSite;
import cn.shanke.torrentmanagerserver.service.BTSiteService;
import cn.shanke.torrentmanagerserver.util.CacheUtil;
import cn.shanke.torrentmanagerserver.util.StringUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/btsite")
public class BTSiteController {
    @Resource
    private BTSiteService modelService;

    @GetMapping("/list")
    public ResponseModel listModels() {
        return ResponseModel.success().data(modelService.list(null));
    }

    @GetMapping(value = "/find/{id}")
    public ResponseModel getModelById(@PathVariable("id")Long id) {
        return ResponseModel.success().data(modelService.getById(id));
    }

    @PostMapping("/save")
    @CacheEvict(value = CacheUtil.CACHE_VALUE_CONSTANTS, key = "'"+ CacheUtil.KEY_PATH_OF_BTSITE + "' + #model.id")
    public ResponseModel saveModel(@RequestBody BTSite model) {
        model.setFolder(StringUtil.removeSidesSlash(model.getFolder()));
        modelService.saveOrUpdate(model);
        return ResponseModel.success().data(model.getId());
    }

    @GetMapping("/delete/{id}")
    @CacheEvict(value = CacheUtil.CACHE_VALUE_CONSTANTS, key = "'"+ CacheUtil.KEY_PATH_OF_BTSITE + "' + #p0")
    public ResponseModel deleteModel(@PathVariable("id")Long id) {
        modelService.removeById(id);
        return ResponseModel.success();
    }

    @GetMapping("/options")
    public ResponseModel options() {
        return ResponseModel.success().data(modelService.list(null));
    }
}
